package com.groep14.manager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.web.servlet.ModelAndView;

import com.groep14.model.ACAdapter;
import com.groep14.model.Battery;
import com.groep14.model.Cpu;
import com.groep14.model.GraphicsCard;
import com.groep14.model.InternalHD;
import com.groep14.model.MyProduct;
import com.groep14.model.Ram;
import com.groep14.project.Connectie;
import com.groep14.project.MyLogger;

/**
 * De VergelijkManager beheert de ModelAndView van de vergelijk pagina.
 * 
 * @since r51 - 14 April 2012
 * @author Maarten Tutak
 *
 */

public class VergelijkManager {

	private ModelAndView modelAndView = new ModelAndView("vergelijken");
	private MyLogger myLogger = new MyLogger();
	
	/**
	 * Constructor van de VergelijkManager, waarbij we twee ID's van laptops binnenkrijgen om te gaan vergelijken met elkaar.
	 * @param ID1
	 * @param ID2
	 */
	
	public VergelijkManager(int ID1, int ID2) {
		
		try {
			Connectie con = new Connectie();
			ResultSet result;
			ArrayList<MyProduct> products = new ArrayList<MyProduct>();
			
			con.setpStatement(con.getCon().prepareStatement("SELECT * FROM Products p JOIN Graphic_Card g ON p.GraphC_id = g.GraphC_ID LEFT OUTER JOIN Battery b ON p.Bat_id = b.Bat_id JOIN internal_hd h ON p.IntHD_id = h.IntHD_id JOIN cpu c ON p.Cpu_id = c.Cpu_id JOIN ac_adapter a ON p.Adap_id = a.Adap_id LEFT OUTER JOIN ram_in_products rip ON p.EAN_UCC_13 = rip.EAN_UCC_13 LEFT OUTER JOIN ram r ON rip.Ram_id = r.Ram_id WHERE p.EAN_UCC_13 in (?,?)"));
			con.getpStatement().setInt(1, ID1);
			con.getpStatement().setInt(2, ID2);
			
			result = con.getpStatement().executeQuery();
			
			while(result.next()) { //2 producten !
				MyProduct p = new MyProduct();
				
				p.setEAN_UCC_13(result.getInt("p.EAN_UCC_13"));
				p.setName(result.getString("p.Name"));
				p.setBusinessEntity(result.getString("BusinessEntity"));
				p.setWeight(result.getFloat("p.Weight"));
				p.setWidth(result.getInt("p.Width"));
				p.setColor(result.getString("p.Color"));
				p.setDescription(result.getString("p.Description"));
				p.setDurationOfWarrantyMonths(result.getInt("p.DurationOfWarrantyMonths"));
				p.setPrice(result.getFloat("p.Price"));
				
				//Constructor van Cpu klasse word opgevuld met resultaten uit query.
				p.setCpu(new Cpu(result.getInt("c.Cpu_id"),result.getString("c.Name"),result.getInt("c.CacheMemory"),result.getString("c.CPUSpeed")));
				//Constructor van InternalHD klasse word opgevuld met resultaten uit query.
				p.setInternal_hd(new InternalHD(result.getInt("IntHD_id"), result.getInt("CapacityOfStorage"), result.getInt("MeanAccessTime"), result.getString("Manufacturer"), result.getString("BusinessEntity"), result.getString("Type"), result.getInt("Cache"), result.getInt("Rpm")));
				//Constructor van Battery klasse word opgevuld met resultaten uit query.
				p.setBattery(new Battery(result.getInt("Bat_id"), result.getInt("Lifespan")));
				//Constructor van ACAdapter klasse word opgevuld met resultaten uit query.
				p.setAcAdapter(new ACAdapter(result.getInt("Adap_id"), result.getInt("Wattage")));
				//Constructor van GraphicsCard klasse word opgevuld met resultaten uit query.
				p.setGraphicsCard(new GraphicsCard(result.getInt("Graphc_id"), result.getString("g.Name"), result.getInt("MemoryCap"), result.getString("CPU"), result.getInt("MaxDataBusSpeed")));
				//Constructor van Ram klasse word opgevuld met resultaten uit query.
				p.setRam(new Ram(result.getInt("Ram_id"), result.getInt("MemoryCapacity"), result.getString("Version"), result.getInt("MaxDataBusSpeed")));
				
				products.add(p);
			}
			
			modelAndView.addObject("products", products);
			
		} catch (SQLException e) {
			myLogger.logException(getClass() + " - ", e);
		} catch (ClassNotFoundException e) {
			myLogger.logException(getClass() + " - ", e);
		} catch (InstantiationException e) {
			myLogger.logException(getClass() + " - ", e);
		} catch (IllegalAccessException e) {
			myLogger.logException(getClass() + " - ", e);
		}
	}

	/**
	 * ModalAndView getter
	 * @return modelAndView
	 */
	public ModelAndView getModelAndView() {
		return modelAndView;
	}

	/**
	 * ModalAndView setter
	 * @param modelAndView
	 */
	public void setModelAndView(ModelAndView modelAndView) {
		this.modelAndView = modelAndView;
	}
	
}
